package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.ClienUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/7/12.
 */
@Controller
@RequestMapping("/api/test/")
public class testController extends ApiController {

    /**
     * @api {post} /api/test/index    考试记录
     * @apiName test.index
     * @apiGroup test
     * @apiParam {String}  token  token
     * @apiSuccess {String} data.rows.total 累计总做题数量
     * @apiSuccess {String} data.rows.passTotal 及格数量
     * @apiSuccess {String} data.rows.examinTotal 考试总数
     * @apiSuccess {String} data.rows.examinId 考试id
     * @apiSuccess {String} data.rows.source 考试分数
     * @apiSuccess {String} data.rows.pass 1及格2不及格
     * @apiSuccess {String} data.rows.createtime 日期
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/index")
    public AjaxResult index(ClienUser user) {
        List<Map> mapList = Db.selectList(" select examinId,count(r.total) as total,r.pass,r.source,e.title,r.createtime from t_examin_record r ,t_examin e where e.id = r.examinId and r.userId = #{userId} group by examinId order by r.createtime desc", getCMap().put("userId", user.getId()));
        Integer total = 0;
        Integer passTotal = 0;
        for (Map map : mapList) {
            total += Convert.toInt(map.get("total"));
            if (Convert.toInt(map.get("pass")) == 1) {
                ++passTotal;
            }
        }
        return json(getCMap().put("total", total).put("passTotal", passTotal).put("examinTotal", mapList.size()).put("list", mapList));
    }

}
